'use strict';

//用户简历相关
angular.module('mean.employ')
    .controller('UserResumeController', function($scope, $http, $state, $ionicPopup) {
        $scope.addResume = {};
        $scope.findJob = function() {
            $state.go('user.findJob');
        };
      
     

        $scope.edit = function(id) {
            $state.go('user.editResume', {resumeId: id});
        };
        $scope.add = function() {
            $http.post('/wx/employ/userResumes', {
                userId: $scope.publicData.userInfo._id,
                resumeData: {
                    resume_name: $scope.addResume.resume_name
                }
            }).then(function(success) {
                return $scope.refreshData();
            }).then(function() {
                $scope.addResume.resume_name = '';
            });
        };
        $scope.delete = function(id) {
            $http.delete('/wx/employ/userResumes', {
                    params: {id: id }
                }   
            ).success(function(data) {
                $scope.refreshData();
            });
        };
        
        $scope.addPopup = function() {
            $scope.publicData.popup = $ionicPopup.show({
                template: '<input type="text" ng-model="addResume.resume_name">',
                title: '请输入简历名称',
                scope: $scope,
                buttons: [{
                    text: '取消'
                }, {
                    text: '<b>确认</b>',
                    type: 'button-positive',
                    onTap: function(e) {
                        return 'add';
                    }
                }]
            });
            $scope.publicData.popup.then(function(success) {
                if(success === 'add') {
                    $scope.add($scope.addResume.resume_name);
                }
            });
        };

        $scope.deletePopup = function(id) {
            $scope.publicData.popup = $ionicPopup.show({
                template: '',
                title: '确认删除简历？',
                subTitle: '注意，该操作无法撤销。',
                scope: $scope,
                buttons: [{
                    text: '取消'
                }, {
                    text: '<b>删除</b>',
                    type: 'button-positive',
                    onTap: function(e) {
                        return 'delete';
                    }
                }]
            });
            $scope.publicData.popup.then(function(success) {
                if(success === 'delete') {
                    $scope.delete(id);
                }
            });
        };


    })
    .controller('EditResumeController', function($scope, $state, $http, $stateParams, $ionicPopup) {
        $scope.$on('$ionicView.afterEnter', function() {
            if($scope.tmp.language || $scope.tmp.work || $scope.tmp.skill || $scope.tmp.education) {
                if($scope.tmp.language) {
                    if(!$scope.tmp.languageIndex && $scope.tmp.languageIndex !== 0) {
                        $scope.tmp.resume.language_info.push($scope.tmp.language);
                    } else {
                        $scope.tmp.resume.language_info[$scope.tmp.languageIndex] = $scope.tmp.language;
                    }
                    $scope.tmp.language = null;
                }
                if($scope.tmp.work) {
                    if(!$scope.tmp.workIndex && $scope.tmp.workIndex !== 0) {
                        $scope.tmp.resume.work_info.push($scope.tmp.work);
                    } else {
                        $scope.tmp.resume.work_info[$scope.tmp.workIndex] = $scope.tmp.work;
                    }
                    $scope.tmp.work = null;
                }
                if($scope.tmp.skill) {
                    if(!$scope.tmp.skillIndex && $scope.tmp.skillIndex !== 0) {
                        $scope.tmp.resume.skill_info.push($scope.tmp.skill);
                    } else {
                        $scope.tmp.resume.skill_info[$scope.tmp.skillIndex] = $scope.tmp.skill;
                    }
                    $scope.tmp.skill = null;
                }
                if($scope.tmp.education) {
                    if(!$scope.tmp.educationIndex && $scope.tmp.educationIndex !== 0) {
                        $scope.tmp.resume.education_info.push($scope.tmp.education);
                    } else {
                        $scope.tmp.resume.education_info[$scope.tmp.educationIndex] = $scope.tmp.education;
                    }
                    $scope.tmp.education = null;
                } 
            } else {
                $scope.refreshData().then(function(success) {
                    $scope.tmp.resume = $scope.publicData.userResumes.filter(function(f) {
                        return f._id === $stateParams.resumeId;
                    })[0];
                    if (!$scope.tmp.resume) {
                        $scope.goHome();
                    }
                });
            }
        });

        $scope.saveResume = function() {
            $http.put('/wx/employ/userResumes', {
                resumeId: $stateParams.resumeId,
                editData: $scope.tmp.resume
            }).then(function(success) {
                $scope.cleanTmpData();
                return $scope.refreshData();
            }).then(function(success) {
                $state.go('user.userResume', {}, {location:'replace'});
            });
        };
        $scope.cancelSaveResume = function() {
            $state.go('user.userResume', {}, {location:'replace'});
        };
        $scope.add = function(type) {
            $scope.tmp[type] = {};
            $state.go('user.editResume' + type[0].toUpperCase() + type.substr(1), $state.params);
        };
        $scope.edit = function(type, index) {
            $scope.tmp[type] = $scope.tmp.resume[type + '_info'][index];
            $scope.tmp[type + 'Index'] = index;
            $state.go('user.editResume' + type[0].toUpperCase() + type.substr(1), $state.params);
        };
        $scope.remove = function(type, index) {
            $scope.tmp.resume[type + '_info'].splice(index, 1);
        };
    })
    .controller('EditResumeLanguageController', function($scope, $state, $http, $stateParams) {
        $scope.save = function(cancel) {
            if(cancel) {$scope.tmp.language = null;}
            $state.go('user.editResume', $state.params, {location:'replace'});
        };
    })
    .controller('EditResumeWorkController', function($scope, $state, $http, $stateParams) {
        $scope.save = function(cancel) {
            if(cancel) {$scope.tmp.work = null;}
            $state.go('user.editResume', $state.params, {location:'replace'});
        };
    })
    .controller('EditResumeSkillController', function($scope, $state, $http, $stateParams) {
        $scope.save = function(cancel) {
            if(cancel) {$scope.tmp.skill = null;}
            $state.go('user.editResume', $state.params, {location:'replace'});
        };
    })
    .controller('EditResumeEducationController', function($scope, $state, $http, $stateParams) {
        $scope.save = function(cancel) {
            if(cancel) {$scope.tmp.education = null;}
            $state.go('user.editResume', $state.params, {location:'replace'});
        };
    })
    .controller('AddResumeController', function($scope, $state, $http) {
        $scope.resumeData = {};
        $scope.saveResume = function() {
            $http.post('/wx/employ/userResumes', {
                userId: $scope.publicData.userInfo._id,
                resumeData: $scope.resumeData
            }).then(function(success) {
                return $scope.refreshData();
            }).then(function(success) {
                $scope.goBack();
            });
        };
    })
;